package com.bdqn.easybuy1.servlet.pre;

import com.bdqn.easybuy1.entities.User;
import com.bdqn.easybuy1.service.user.UserService;
import com.bdqn.easybuy1.service.user.UserServiceImpl;
import com.bdqn.easybuy1.servlet.AbstractServlet;
import com.bdqn.easybuy1.utils.EmptyUtils;
import com.bdqn.easybuy1.utils.ReturnResult;
import com.bdqn.easybuy1.utils.SecurityUtils;
import com.google.code.kaptcha.Constants;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "LoginServlet", value = "/Login")
public class LoginServlet extends AbstractServlet {

    private UserService userService;

    @Override
    public void init() throws ServletException {
        userService = new UserServiceImpl();
    }

    /**
     * 跳转到登陆界面
     *
     * @param request
     * @param response
     * @return
     */
    public String toLogin(HttpServletRequest request, HttpServletResponse response) throws Exception {
        return "/pre/login";
    }

    /**
     * 登陆的方法
     *
     * @param request
     * @return
     */
    public ReturnResult login(HttpServletRequest request, HttpServletResponse response) throws Exception {
        HttpSession session = request.getSession();
        String validateCode = (String) session.getAttribute(Constants.KAPTCHA_SESSION_KEY);
        session.removeAttribute(Constants.KAPTCHA_SESSION_KEY);

        ReturnResult result = new ReturnResult();
        if (EmptyUtils.isEmpty(validateCode)) {
            // 服务端验证码为空
            result.returnFail("服务端验证码不存在，请刷新");
        } else {
            // 参数获取
            String code = request.getParameter("code");
            if (EmptyUtils.isEmpty(code)) {
                result.returnFail("验证码不能为空");
            } else {
                if (code.equalsIgnoreCase(validateCode)) {
                    String loginName = request.getParameter("loginName");
                    String password = request.getParameter("password");
                    User user = userService.getUser(null, loginName);
                    if (EmptyUtils.isEmpty(user)) {
                        result.returnFail("用户不存在");
                    } else {
                        if (user.getPassword().equals(SecurityUtils.md5Hex(password))) {
                            //登陆成功
                            session.setAttribute("loginUser", user);
                            result.returnSuccess("登陆成功");
                        } else {
                            result.returnFail("密码错误");
                        }
                    }
                }else{
                    result.returnFail("验证码错误");
                }
            }
        }
        return result;
    }


    /**
     * 登陆的方法
     *
     * @param request
     * @return
     */
    public String loginOut(HttpServletRequest request, HttpServletResponse response) throws Exception {
        try {
            User user = (User) request.getSession().getAttribute("loginUser");
            request.getSession().removeAttribute("loginUser");
            // 清除购物车
            request.getSession().removeAttribute("cart");
            request.getSession().removeAttribute("cart2");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "/pre/login";
    }
}
